import Vue from 'vue';
import Router from 'vue-router';

// import Login from './components/Login.vue';
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Login.vue')
// import Home from './components/Home.vue';
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Home.vue')
// import Welcome from './components/Welcome.vue';
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Welcome.vue')

// import Users from './components/user/Users.vue';
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ './components/user/Users.vue')
// import Roles from './components/power/Roles.vue';
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ './components/power/Roles.vue')
// import Rights from './components/power/Rights.vue';
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ './components/power/Rights.vue')

// import Categories from './components/goods/Categories.vue'
const Categories = () => import(/* webpackChunkName: "Cate_Params" */ './components/goods/Categories.vue')
// import GoodsParams from './components/goods/GoodsParams.vue'
const GoodsParams = () => import(/* webpackChunkName: "Cate_Params" */ './components/goods/GoodsParams.vue')


// import List from './components/goods/List.vue'
const List = () => import(/* webpackChunkName: "GoodsList_Add" */ './components/goods/List.vue')
// import Add from './components/goods/Add.vue'
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ './components/goods/Add.vue')

// import Order from './components/order/Order.vue'
const Order = () => import(/* webpackChunkName: "Order_Report" */ './components/order/Order.vue')
// import report from './components/report/report.vue'
const report = () => import(/* webpackChunkName: "Order_Report" */ './components/report/report.vue')





Vue.use(Router);

let router = new Router({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home', component: Home, redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
                { path: '/roles', component: Roles },
                { path: '/rights', component: Rights },
                { path: '/categories', component: Categories },
                { path: '/params', component: GoodsParams },
                { path: '/goods', component: List },
                { path: '/goods/add', component: Add },
                { path: '/orders', component: Order },
                { path: '/reports', component: report },
            ]
        },
    ]
})

// 挂载路由导航守卫
// 为路由对象添加beforeEach导航守卫
router.beforeEach((to, from, next) => {
    // 如果用户访问的是登录页,直接放行
    if (to.path === '/login') return next();
    // 从sessionStorage中获取到保存的token值
    let tokenStr = window.sessionStorage.getItem('token');
    // 如果没有token, 则表示用户没有登录, 强制跳转到登录页
    if (!tokenStr) return next('/login');
    next();
})

// 重复频繁多次点击二级菜单,路由组件会报错,这里用捕获错误,不让其报错
// 老师写的代码,不要求会
// const originalPush = Router.prototype.push
// Router.prototype.push =  function push(location) {
//     return originalPush.call(this, location).catch(err => err)
// }


export default router